﻿using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Domain.BusinessManage;
using SmartWMS.Server.Repository;
using SmartWMS.Server.StatementServer.AvailableStockServices.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartWMS.Server.StatementServer.AvailableStockServices
{
    /// <summary>
    /// 可出库存服务
    /// </summary>
    public class AvailableStockServices : IAvailableStockServices
    {
        private readonly IRepository<Unit> _unit;
        private readonly IRepository<Product> _product;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="unit"></param>
        /// <param name="product"></param>
        public AvailableStockServices(IRepository<Unit> unit, IRepository<Product> product)
        {
            _unit = unit;
            _product = product;
        }

        /// <summary>
        ///可出库存列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<List<AvailableStockDto>>> GetAvailableStockList(AvailableStockSelectDto input)
        {
            //销售退货表
            var UnitList = await _unit.GetAllListAsync();
            //销售管理表
            var ProductList = await _product.GetAllListAsync();

            var list = (from a in UnitList
                        join b in ProductList on a.Id equals b.Id
                        select new AvailableStockDto
                        {
                            Id = a.Id,
                            ProductName = b.ProductName,
                            UnitName = a.UnitName,
                            Inventory = b.Inventory,
                            ProductCode = b.ProductCode,
                            ProductTypeId = b.ProductTypeId,
                            Specs = b.Specs
                        }).ToList() ;


            if (!string.IsNullOrEmpty(input.ProductCode))
            {
                list = list.Where(x => x.ProductCode.Contains(input.ProductCode)).ToList();
            }
            if (!string.IsNullOrEmpty(input.ProductName))
            {
                list = list.Where(x => x.ProductName.Contains(input.ProductName)).ToList();
            }

            //总数量
            int totalCount=list.Count();
            int pageCount = (int)Math.Ceiling(totalCount*1.0 / input.pageSize);

            list = list.Skip((input.pageIndex - 1) * input.pageSize).Take(input.pageSize).ToList();

            return new Response<List<AvailableStockDto>>()
            {
                Data = list,
                TotalCount = totalCount,
                PageCount = pageCount
            };

        }
    }
}
